//
//  ImageDetailView.m
//  WWXELuShang
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "ImageDetailView.h"
#import "ImageDetailModel.h"

@implementation ImageDetailView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLabel];
        [self addSubview:self.detailLabel];
        self.backgroundColor = [UIColor blackColor];

    }
    
    
    return self;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth / 40, kHeight * 0.75, kWidth - kWidth / 20, kWidth / 18.75)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:kWidth / 18.75];
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}
- (UILabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth / 40, kHeight * 0.75+ kWidth / 18.75 + kWidth / 40, kWidth - kWidth / 20, kWidth / 18.75)];
        _detailLabel.font = [UIFont systemFontOfSize:kWidth / 24];
        _detailLabel.numberOfLines = 0;
        _detailLabel.textColor = [UIColor whiteColor];
    }
    
    return _detailLabel;
}

- (void)setValueWithScrollView:(NSArray *)array target:(id)target {
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.delegate = target;
    self.scrollView.contentSize = CGSizeMake(kWidth * array.count, 0);
    self.scrollView.pagingEnabled = YES;
    
    for (int i = 0; i < array.count; i ++) {
        ImageDetailModel *imageDetail = array[i];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kWidth * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        [self.scrollView addSubview:view];
        
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageDetail.imageUrl]]];
        CGFloat height = image.size.height * kWidth / image.size.width;
        CGSize size = CGSizeMake(kWidth, height);
        UIImage *newImage = [self image:image byScalingToSize:size];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:newImage];
        imageView.center = self.scrollView.center;
        [view addSubview:imageView];
    }
    [self insertSubview:self.scrollView atIndex:0];

    
    
}

- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    UIGraphicsBeginImageContext(targetSize);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width  = targetSize.width;
    thumbnailRect.size.height = targetSize.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}


- (void)setDetail:(NSString *)detail {
    self.detailLabel.text = detail;
    
    CGRect rect = [self heightOfLabel:self.detailLabel];
    
    CGRect frame = self.detailLabel.frame;
    
    frame.size.height = rect.size.height;
    
    self.detailLabel.frame = frame;
    
}


//自定义高度
- (CGRect)heightOfLabel:(UILabel *)label{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:label.font forKey:NSFontAttributeName];
    CGRect rect =[label.text boundingRectWithSize:CGSizeMake(kWidth - kWidth / 20, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return rect;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
